/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.eurotrader.componentes;

import br.com.eurotrader.interfaces.InterfaceControllerCollection;
import br.com.eurotrader.interfaces.InterfaceEntidades;
import br.com.eurotrader.utils.FacesUtils;
import br.com.eurotrader.utils.MetodosBotoes;
import com.icesoft.faces.component.ext.HtmlDataTable;
import com.icesoft.faces.component.ext.RowSelectorEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.faces.event.ActionEvent;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.inject.Named;

/**
 *
 * @author Gilson Ricardo
 */
@Named
public class BeanDataTable {

    private Map<Integer, Boolean> dataTableOneRowSelected = new HashMap<Integer, Boolean>();

    public Boolean getDataTableOneRowSelected() {
        //System.out.println("GET "+getDataTable1().getRowIndex());
        // System.out.println("** "+dataTableOneRowSelected);

          //InterfaceControllerCollection interfaceController = (InterfaceControllerCollection) ((MetodosBotoes) Beans.getBean("botoes")).getControllerPagina();



        int currentRow = getDataTable1().getRowIndex();
        Boolean returnValue = Boolean.FALSE;
        if (dataTableOneRowSelected.containsKey(currentRow)) {
            returnValue = dataTableOneRowSelected.get(currentRow);
        }
        return returnValue;
    }

    public void setDataTableOneRowSelected(Boolean selected) {
        //  System.out.println("SET "+getDataTable1().getRowIndex());
        //  InterfaceControllerCollection interfaceController = (InterfaceControllerCollection) ((MetodosBotoes) Beans.getBean("botoes")).getControllerPagina();

        int currentRow = getDataTable1().getRowIndex();
        Boolean returnValue = Boolean.FALSE;
        if (dataTableOneRowSelected.containsKey(currentRow)) {
            //   System.out.println("$");
            returnValue = dataTableOneRowSelected.get(currentRow);
            if ((selected) && (returnValue)) {
                // System.out.println("$$");
                dataTableOneRowSelected.put(currentRow, false);
            } else {
                // System.out.println("$$$");
                dataTableOneRowSelected.put(currentRow, selected);
            }
        } else {
            //  System.out.println("$ ELSE");
            dataTableOneRowSelected.put(currentRow, selected);
        }
    }

    public void listenerSelecaoTabela(RowSelectorEvent rse) {
        //  System.out.println("executei aqui");
        int selectedRowIndex = rse.getRow();
        getDataModelTabela().setRowIndex(selectedRowIndex);

        //setObjeto((T) getDataModelTabela().getRowData());

    }
    private DataModel dataModelTabela = null;

    public DataModel getDataModelTabela() {

        InterfaceControllerCollection interfaceController = (InterfaceControllerCollection) ((MetodosBotoes) FacesUtils.getManagedBean("botoes")).getControllerPagina();
//  parei aqui agora tem q passar a lista para a tabela
        //((MetodosBotoes) Beans.getBean("botoes")).getControllerPagina();
//        return dataModelTabela = new ListDataModel( cadGerUsuario.);
        return dataModelTabela = new ListDataModel((List) interfaceController.getCollection());
    }

    public void setDataModelTabela(DataModel dataModelTabela) {

        //  System.out.println("Setando");
        this.dataModelTabela = dataModelTabela;
    }
    private HtmlDataTable dataTable1 = new HtmlDataTable();

    public HtmlDataTable getDataTable1() {
        return dataTable1;
    }

    public void setDataTable1(HtmlDataTable dataTable1) {
        this.dataTable1 = dataTable1;
    }
    InterfaceEntidades itemSelecionado;

    public InterfaceEntidades getItemSelecionado() {
        return itemSelecionado;
    }

    public void setItemSelecionado(InterfaceEntidades itemSelecionado) {
        this.itemSelecionado = itemSelecionado;
    }

    public void adicionarItem(ActionEvent event) {

        InterfaceControllerCollection interfaceController = (InterfaceControllerCollection) ((MetodosBotoes) FacesUtils.getManagedBean("botoes")).getControllerPagina();

        Collection collec = interfaceController.getCollection();

        System.out.println("ADD ITEM");

        if (collec == null) {
            collec = (new ArrayList());
        }


        if (itemSelecionado != null) {

            if (collec.contains(itemSelecionado)) {
                if (event.getComponent() != null) {
                    FacesUtils.addInfoMessage(event.getComponent().getClientId(), "Registro Duplicado. Item já selecionado.");
                }
                return;
            } else {
                collec.add(itemSelecionado);

            }

            interfaceController.setCollection(collec);

        }

    }
    private ArrayList<InterfaceEntidades> listaExcluir = null;

    public ArrayList<InterfaceEntidades> getListaExcluir() {
        if (listaExcluir == null) {
            listaExcluir = new ArrayList<InterfaceEntidades>();
        }
        return listaExcluir;
    }

    public void setListaExcluir(ArrayList<InterfaceEntidades> listaExcluir) {
        this.listaExcluir = listaExcluir;
    }

    public void excluirItem(ActionEvent event) {
        //frag.preencheListaDeDiasExcluir(getListaDiasExcluir());

        InterfaceControllerCollection interfaceController = (InterfaceControllerCollection) ((MetodosBotoes) FacesUtils.getManagedBean("botoes")).getControllerPagina();

        if ((interfaceController.getCollection() != null)) {

            for (Iterator iter = dataTableOneRowSelected.keySet().iterator(); iter.hasNext();) {
                Integer linha = (Integer) iter.next();

                if (dataTableOneRowSelected.get(linha)) {
                    //getDataTable1().setRowIndex(linha);
                    dataModelTabela.setRowIndex(linha);
                    if (listaExcluir == null) {
                        listaExcluir = new ArrayList<InterfaceEntidades>();
                    }
                    listaExcluir.add((InterfaceEntidades) dataModelTabela.getRowData());

                }
            }

            Collection c = interfaceController.getCollection();

            for (InterfaceEntidades diasExcluir : listaExcluir) {

                if (c.contains(diasExcluir)) {

                    c.remove(diasExcluir);

                }

            }
            interfaceController.setCollection(c);


            listaExcluir.clear();
            dataTableOneRowSelected.clear();

        }

    }

    public void limparTabela(ActionEvent event) {


        InterfaceControllerCollection interfaceController = (InterfaceControllerCollection) ((MetodosBotoes) FacesUtils.getManagedBean("botoes")).getControllerPagina();
        System.out.println("LIMPAR " + interfaceController.getCollection());
        if (interfaceController.getCollection() != null) {
            Collection c = interfaceController.getCollection();
            c.clear();

            dataTableOneRowSelected.clear();
            interfaceController.setCollection(c);

        }
    }
//   public void limpaLinhas() {
//        dataTableOneRowSelected.clear();
//        getDataTable1().setRowIndex(-1);
//    }
}
